package com.jsyl.lwbk.common.utils;

import java.net.URL;

/**
 * url工具类
 * @author gby
 * @version 1.0
 * @date 2019/3/8 0008.
 */
public class UrlUtil {

    private final static String URL_SEPARATOR = "/";

    /**
     * 拼接url
     * @param url
     * @param path
     * @return
     */
    public static String joinPath(String url,String path){
        if(StringUtils.isBlank(path)){
            return url;
        }

        if(url.endsWith(URL_SEPARATOR)){
            return url + path;
        }else {
            return url + URL_SEPARATOR + path;

        }
    }

    /**
     * 拼接url
     *
     * @param url  xxx
     * @param paths xxx
     * @return  xxx/xxx/
     */
    public static String joinPathWithEndSeparator(String url,String ... paths){
        if(paths == null || paths.length == 0){
            return url;
        }

        for(String path : paths){
            if(!url.endsWith(URL_SEPARATOR) && !path.startsWith(URL_SEPARATOR)){
                url += URL_SEPARATOR;
            }
            url += path;
        }

        if(!url.endsWith(URL_SEPARATOR)){
            url += URL_SEPARATOR;
        }
        return url;
    }

    public static String  getHostName(String  url) {
        try {
            if(!url.startsWith("http")){
                url = "http://"+url;
            }

            URL urlObject = new URL(url);
            return urlObject.getHost();
        }catch (Exception e){
        }
        return url;
    }
}
